 
<head>
    <meta charset="utf8">
</head>	
 
 <?php
	include_once "../init.php";
	include_once "../util/mysql_class.php";
	include_once "../util/util.php";
	include_once "../third_part/simple_html_dom.php";
	include_once "Parser.class.php";
	
	function pickURL($seed_id,$seed_url,$category_id,$template_file){
		$db =  new mysql();	
		//读远程url
		//$chplst = pick_news_by_seed($seed_url);
		$chplst = route($seed_url,$template_file);
		//写数据库
		//print_r($chplst);
		
		foreach($chplst as $id=>$chp){
			$articl_url=$chp["news_url"];
			if(!is_news_exsit($articl_url)){
				$insert_sql="INSERT INTO news_info(seed_id,news_title,news_url,picked,category_id)VALUES (".$seed_id.",'".$chp["news_title"]."','".$articl_url."',0,".$category_id.")";
				$db->query($insert_sql);
			}
		}
		
		$update_sql="update news_seed set last_update='".date('YmdHis')."' where seed_id=".$seed_id;
		echo $update_sql;
		$db->query($update_sql);
		
		echo "采集完成";
	}
	
	/**
		判断文章是否采集过
	**/
	function is_news_exsit($url){
		$db =  new mysql();
		$sql_select="select news_id from news_info where news_url='".$url."'";
		$query = $db->query($sql_select);
		if($db->num_rows($query)>0){
			return true;
		}
		return false;
	}
	
	function route($seed_url,$template_file){
		$parserBean = new $template_file($seed_url);
		return $parserBean->parse();
	}
	
	/*function pick_news_by_seed($seed_url){
		$contents = myfile_get_content($seed_url);
		$html = str_get_html($contents);
		foreach($html->find('div.Q-tpList h3 a') as $article) {
			$item['news_title']     =  mb_convert_encoding($article->innertext,"utf8","gb2312");
		    $item['news_url']     =  $article->href;
		    $articles[] = $item;
		}
		return $articles;
	}*/
	
	if(@$_GET["seedid"]){
		$seedid=$_GET["seedid"];
		$sql_select="select seed_id,seed_url,category_id,template_file from news_seed where seed_id=".$seedid;
	}else{
		//用来动态更新，选取超过10分钟没有更新的种子
		$sql_select="select seed_id,seed_url,category_id,template_file from news_seed where DATE_ADD(last_update,INTERVAL '10:0' MINUTE_SECOND)<'".date('YmdHis')."' limit 0,1";
	}
	
	$db =  new mysql();
	$query = $db->query($sql_select);
	if($db->num_rows($query)>0){
		$seed = $db->fetch_row_array($query);
		$seed_url=$seed["seed_url"];
		$seed_id=$seed["seed_id"];
		$category_id=$seed["category_id"];
		$template_file=$seed["template_file"];
		pickURL($seed_id,$seed_url,$category_id,$template_file);
	}
	
?>